package com.baozun.houji.goods.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.R
import com.baozhun.mall.common.adapter.CommonGoodsChannelAdapter
import com.baozhun.mall.common.adapter.CommonGoodsSelfAdapter
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.databinding.ViewHotRecomendBinding
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.util.Constants
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.fondesa.recyclerviewdivider.dividerBuilder
import com.fondesa.recyclerviewdivider.staggeredDividerBuilder
import me.hgj.jetpackmvvm.ext.util.screenWidth

/**
 * @Title：热卖推荐view
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/13
 * @Version：1.0.0
 */
class HotRecommendView(context: Context, private var attrs: AttributeSet? = null) : LinearLayout(context, attrs),
    OnItemClickListener {

    private var mBinding: ViewHotRecomendBinding? = null
    private var mIsSelf: Boolean = false

    init {
        initView()
    }

    fun initView() {
        if (isInEditMode) return
        val array = context.obtainStyledAttributes(attrs, R.styleable.RecommendView)
        mIsSelf = array.getBoolean(R.styleable.RecommendView_isSelf, false)
        mBinding = DataBindingUtil.bind(View.inflate(context, R.layout.view_hot_recomend, null))
        setBackgroundColor(ContextCompat.getColor(context, com.baozhun.mall.res.R.color.white))
        addView(mBinding?.root)
        array.recycle()
    }

    /**
     * 设置数据
     */
    fun setData(goodsList: MutableList<GoodsInfoBean>) {
        setData(goodsList, false)
    }

    /**
     * 设置数据
     */
    fun setData(goodsList: MutableList<GoodsInfoBean>, isSelf: Boolean = true) {
        if (isSelf) {
            mBinding?.rvRecommend?.layoutManager = LinearLayoutManager(context)
            mBinding?.rvRecommend?.addItemDecoration(
                context.dividerBuilder()
                    .size(SizeUtils.dp2px(8f))
                    .color(ContextCompat.getColor(context, com.baozhun.mall.res.R.color.color_F7F7F7))
                    .build()
            )
            mBinding?.rvRecommend?.adapter = CommonGoodsSelfAdapter(goodsList, onItemClickListener = this)
            if (mBinding?.rvRecommend?.layoutParams is MarginLayoutParams) {
                (mBinding?.rvRecommend?.layoutParams as MarginLayoutParams).setMargins(SizeUtils.dp2px(8f), SizeUtils.dp2px(10f), SizeUtils.dp2px(8f), 0)
            }
        } else {
            mBinding?.rvRecommend?.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            mBinding?.rvRecommend?.addItemDecoration(
                context.staggeredDividerBuilder()
                    .size(SizeUtils.dp2px(7f))
                    .color(ContextCompat.getColor(context, com.baozhun.mall.res.R.color.color_F7F7F7))
                    .build()
            )
            if (mBinding?.rvRecommend?.layoutParams is MarginLayoutParams) {
                (mBinding?.rvRecommend?.layoutParams as MarginLayoutParams).setMargins(0, SizeUtils.dp2px(10f), 0, 0)
            }
            mBinding?.rvRecommend?.adapter = CommonGoodsChannelAdapter(goodsList, onItemClickListener = this)
        }
        mBinding?.tvRelevantRecommend?.setBackgroundColor(ContextCompat.getColor(context, com.baozhun.mall.res.R.color.white))
    }

    fun setIsSelf(isSelf: Boolean) {
        mIsSelf = isSelf
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        setWidth()
    }

    /**
     * 重新设置view的宽度为屏幕宽度
     */
    private fun setWidth() {
        //设置宽度为屏幕的宽度
        layoutParams?.width = context?.screenWidth
        mBinding?.root?.layoutParams?.width = context?.screenWidth
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if (adapter is CommonGoodsSelfAdapter) {
            ARouter.getInstance().build(RoutePath.Home.GOODS_DETAIL)
                .withString(Constants.Goods.GOODS_ID, adapter?.getItem(position)?.goods_id)
                .withString(Constants.Goods.GOODS_SOURCE, adapter?.getItem(position)?.source)
                .navigation()
        } else if (adapter is CommonGoodsChannelAdapter) {
            ARouter.getInstance().build(RoutePath.Home.GOODS_DETAIL)
                .withString(Constants.Goods.GOODS_ID, adapter?.getItem(position)?.goods_id)
                .withString(Constants.Goods.GOODS_SOURCE, adapter?.getItem(position)?.source)
                .navigation()
        }
    }
}